//

// Funcions per a manipular un camp de data a partir d'un calendari dibuixat en pantalla

//

 

var month; var day; var year, actualDay, actualMonth, actualYear; 

var delim = new Array(":","/","\\","-"," ",".");

var monthArray = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);

 

//

// Inicia el proc?s de selecci? de data:

//

 

var today = new Date();

var hora;

var minuto;

 
function prueba(form) {	
alert("hola");
}
 

function valida_fecha(campo1, mssg)

{

      if(campo1.value!=""){

            if(!verifDate(campo1)) {

                  campo1.focus();

                  today = new Date();

                  thisYear = y2k(today.getYear());

                  thisMonth = today.getMonth()+1;

                  thisDay = today.getDate();

                  campo1.value=thisDay + "/" + thisMonth + "/" + thisYear;

                  alert(mssg);

                  return;

            }

      }

}

 

function y2k(number) { 

      return (number < 1000) ? number + 1900 : number;

}

 

function inicializa_fecha(formObject,fecha,valor){

      today = new Date();

      thisYear = y2k(today.getYear())-valor;

      thisMonth = today.getMonth()+1;

      thisDay = today.getDate();

      document[formObject][fecha].value=thisDay + "/" + thisMonth + "/" + thisYear;

}

 

function verifDate(_fecha1){

      var err=false;

      fecha1=_fecha1.value;

      if (fecha1.length > 10 || fecha1.length < 8)   

            err=true;

      

      var a=fecha1.split("/");

      d = a[0]; // day

      b = a[1]; // month

      f = a[2]; // year

            

      if (d==null||b==null||f==null)     

            err=true;

      if (b<1 || b>12) 

            err = true;

      if (d<1 || d>31) 

            err = true;

      if (f<1900 || f>2050) 

            err = true;

      if (b==4 || b==6 || b==9 || b==11)

            if (d==31) 

                  err=true;

      if (b==2){

            var g=parseInt(f/4);

            if (isNaN(g)) 

                  err=true;

            if (d>29) 

                  err=true;

            if (d==29 && ((f/4)!=parseInt(f/4))) 

                  err=true;

      }

      if(err==false && validaEsNumerico(d)==false)

            err=true;

      if(err==false && validaEsNumerico(b)==false)

            err=true;

      if(err==false && validaEsNumerico(f)==false)

            err=true;

      return !err;

}

 

function validaEsNumerico(valor){

      filtro = "0123456789";  

      for(i=0; i<valor.length; i++) {

            if(filtro.indexOf(valor.charAt(i)) == -1 ) {

                  return false;

            }           

    }

      return true;

}

 

 

function dlgDate (formObject,fieldName, mssg)

{

 var a = document[formObject][fieldName].value;

 if (a == "" || a == "Todas") {  

  var thisYear = y2k(today.getYear());

  var thisMonth = today.getMonth()+1;

  var thisDay = today.getDate();

  

  a = thisDay + "/" + thisMonth + "/" + thisYear; 

  document[formObject][fieldName].value = a;   

 }

  

 var b = a.split("/");

 var fecha;

 

 if(valida_fecha(document[formObject][fieldName]), mssg) {

      if (b[0].length == 1) b[0] = "0" + b[0];

      if (b[1].length == 1) b[1] = "0" + b[1];

      if (b[2].length == 1) b[2] = "0" + b[2];

      fecha = b[0]+b[1]+b[2];

      day = b[0];

      month = b[1];

      year = b[2];

 

      document[formObject][fieldName].text = fecha;

      getMonth_and_Date(formObject,fieldName);

      putcal(formObject,fieldName); 

 }

}

 

//

// Retorna la data actual i assigna el dia, el mes i l'any a les variables globals:

//

 

function getToday() {

   day = today.getDate();   

   month = today.getMonth();

   month++;

   year = today.getYear();

   year = (year < 100) ? 1900 + year : year;

}

 

//

// Llegeix el contingut del camp que se li passa per par?metre i assigna el dia, mes i any indicats en ell

// a les variables globals

//

 

function getMonth_and_Date(formObject,fieldName) {

      dtString = document[formObject][fieldName].value;    

    //trim date string

   while ((dtString.charAt(0) == " ") && (dtString.length != 0))

      dtString = dtString.substring(1,dtString.length - 1)

   while ((dtString.charAt(dtString.length - 1) == " ") && (dtString.length != 0))

      dtString = dtString.substring(0,dtString.length - 1)

 

   //get date components

   i = 0; startPos = 0; pos = 0;   

   //get day

  do {

      pos = dtString.indexOf(delim[i], startPos);      

      i++

   } while ((pos == -1) && (i < delim.length));

   if (pos == -1){

      getToday();

      return;

   }

   day = parseInt(dtString.substring(startPos,pos),10);   

   actualDay=day;

   startPos = pos + 1;

   if ((day < 1) || (day > monthArray[month])){

      getToday();

      return;

   }

      

   //get month

   i = 0;

   do {

      pos = dtString.indexOf(delim[i], startPos);

      i++

   } while ((pos == -1) && (i < delim.length));

   if (pos == -1){//there's no month

      getToday(); 

      return;

   }

   month = parseInt(dtString.substring(startPos,pos),10) - 1;

   actualMonth=month;

   startPos = pos + 1;

   if ((month < 0) || (month > 12)){ //no valid month

      getToday();

      return;

   }

   else month++; 

 

   //get year

   year = parseInt(dtString.substring(startPos,dtString.length),10)

   year = (year < 100) ? 1900 + year : year;

   actualYear=year;

}//getMonth_and_Date

 

//

// Assigna valor al camp de data segons el qu? hagi seleccionat l'usuari al calendari:

//

 

function putcal(formObject,dateFieldName) {

   calwin = open('','calwin','top=200,left=120,width=250,height=180,scrollbars=no,titlebar=no,status=no',false);

   formulario = document.forms[formObject];

   calccal(calwin,formulario,dateFieldName);

}

 

 

//

// Calcula el darrer dia del mes

//

 

function calclastday(month,year) {

 

   if ((month==2) && ((year%4)==0))

      return 29;

   if ((month==2) && ((year%4)!=0))

      return 28;

   if ((month==1) || (month == 3) || (month == 5) || (month == 7) || (month==8) || (month == 10) || (month ==12))

      return 31;

   return 30;

}

 

//

// Dibuixa el calendari a pantalla

//

 

function calccal(targetwin,form,dateFieldName) { 

 

var monthname = new Array(12);

var dayname = new Array(7);

monthname[0] = "Enero";

monthname[1] = "Febrero";

monthname[2] = "Marzo";

monthname[3] = "Abril";

monthname[4] = "Mayo";

monthname[5] = "Junio";

monthname[6] = "Julio";

monthname[7] = "Agosto";

monthname[8] = "Septiembre";

monthname[9] = "Octubre";

monthname[10] = "Noviembre";

monthname[11] = "Diciembre";

 

var endday = calclastday(eval(month),eval(year));

mystr = month + "/01/" + year;

mydate = new Date(mystr);

firstday = mydate.getDay();

firstday = firstday-1;

if (firstday<0) firstday=6;

var cnt = 0;

var day = new Array(6);

for (var i=0; i<6; i++) {

   day[i] = new Array(7);

}

 

for (var r=0; r<6; r++) {

   for (var c=0; c<7; c++) {

      if ((cnt==0) && (c!=firstday)) continue;

      cnt++;

      day[r][c] = cnt;

      if (cnt==endday) break;

   }

   if (cnt==endday) break;

}

 

var prevyear = eval(year) - 1;

var nextyear = eval(year) + 1; 

var nextmonth = (month == 12) ? 1 : month + 1;

var nextmonthyear = (month == 12) ? year + 1 : year;

var prevmonth = (month == 1) ? 12 : month - 1;

var prevmonthyear = (month == 1) ? year - 1 : year;

 

 

targetwin.focus();

targetwin.document.open()

targetwin.document.writeln("<html>");

targetwin.document.writeln("<head>");

targetwin.document.writeln("<title>Calendario</title>");

targetwin.document.writeln("<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>");

targetwin.document.writeln("<link rel=stylesheet href=WEB-CONTENT/css/estilosCalendario.css type=text/css>");

targetwin.document.writeln("</head>");

targetwin.document.writeln("<body>");

targetwin.document.writeln("<form name=form1 id=form1>");

targetwin.document.writeln("<table width=220 border=0 cellspacing=0 cellpadding=0>");

targetwin.document.writeln("  <tr>");

targetwin.document.writeln("    <td valign=top align=center class=BoxVoid>");

targetwin.document.writeln("      <table width=195 border=0 cellspacing=0 cellpadding=0>");

targetwin.document.writeln("        <tr>");

targetwin.document.writeln("          <td>");

targetwin.document.writeln("            <table width=195 border=0 cellspacing=2 cellpadding=0>");

targetwin.document.writeln("              <tr bgcolor=#265674>");

targetwin.document.writeln("                <td width=12><a href=#><img src=WEB-CONTENT/resources/calendario_ant.gif width=12 height=18 alt=Mes Anterior border=0 onclick='opener.month = " + month + "; opener.year = " + prevyear + ";document.clear();opener.calccal(opener.calwin,opener.document." + form.name + ",\"" + dateFieldName + "\")'></a></td>");

targetwin.document.writeln("                <td bgcolor=#265674 align=center valign=middle class=calendario_cabecera>" + year + "</td>");

targetwin.document.writeln("                <td width=12><a href=#><img src=WEB-CONTENT/resources/calendario_sig.gif width=12 height=18 alt=Mes Siguiente border=0 onclick='opener.month = " + month + "; opener.year = " + nextyear + ";document.clear();opener.calccal(opener.calwin,opener.document." + form.name + ",\"" + dateFieldName + "\")'></a></td>");

targetwin.document.writeln("              </tr>");

targetwin.document.writeln("            </table>");

targetwin.document.writeln("          </td>");

targetwin.document.writeln("        </tr>");

targetwin.document.writeln("        <tr>");

targetwin.document.writeln("          <td>");

targetwin.document.writeln("            <table width=195 border=0 cellspacing=2 cellpadding=0>");

targetwin.document.writeln("              <tr bgcolor=#265674>");

targetwin.document.writeln("                <td width=12><a href=#><img src=WEB-CONTENT/resources/calendario_ant.gif width=12 height=18 alt=Mes Anterior border=0 onclick='opener.month = " + prevmonth + "; opener.year = " + prevmonthyear + ";document.clear();opener.calccal(opener.calwin,opener.document." + form.name + ",\"" + dateFieldName + "\")'></a></td>");

 

var index = eval(month) - 1;

 

targetwin.document.writeln("                <td bgcolor=#265674 align=center valign=middle class=calendario_cabecera>" + monthname[index] + "</td>");

targetwin.document.writeln("                <td width=12><a href=#><img src=WEB-CONTENT/resources/calendario_sig.gif width=12 height=18 alt=Mes Siguiente border=0 onclick='opener.month = " + nextmonth + "; opener.year = " + nextmonthyear + ";document.clear();opener.calccal(opener.calwin,opener.document." + form.name + ",\"" + dateFieldName + "\")'></a></td>");

targetwin.document.writeln("              </tr>");

targetwin.document.writeln("            </table>");

targetwin.document.writeln("          </td>");

targetwin.document.writeln("        </tr>");

targetwin.document.writeln("        <tr>");

targetwin.document.writeln("          <td>");

targetwin.document.writeln("            <table width=100% border=0 cellspacing=2 cellpadding=0 class=DocItemsTd3>");

targetwin.document.writeln("              <tr align=center>");

targetwin.document.writeln("                <td class=calendario_dia>Lu</td>");

targetwin.document.writeln("                <td class=calendario_dia>Ma</td>");

targetwin.document.writeln("                <td class=calendario_dia>Mi</td>");

targetwin.document.writeln("                <td class=calendario_dia>Ju</td>");

targetwin.document.writeln("                <td class=calendario_dia>Vi</td>");

targetwin.document.writeln("                <td class=calendario_dia>Sa</td>");

targetwin.document.writeln("                <td class=calendario_dia>Do</td>");

targetwin.document.writeln("              </tr>");

targetwin.document.writeln("              <tr>");

targetwin.document.writeln("                <td bgcolor=#265674 colspan=7 height=1></td>");

targetwin.document.writeln("              </tr>");

 

var selectedmonth = eval(month) - 1;

var today = new Date();

var thisyear = today.getYear() + 1900;

var selectedyear = eval(year) - thisyear + 4;

var conditionalpadder = "";

 

for(r=0; r<6; r++){

      targetwin.document.writeln("<tr align=center>");

      for(c=0; c<7; c++){

            if ( (actualDay==day[r][c]) & (actualMonth==selectedmonth) & (actualYear==eval(year)) ){

                  cellClass="calendario_numerosel"

            }

            else{

                  if ( (c>4) & (day[r][c] != null) ){

                        cellClass="calendario_numero";

                  }

                  else {

                        cellClass="calendario_numero";

                  }

            }

            targetwin.document.writeln("<td class=" + cellClass + ">");

            if(day[r][c] != null) {

                  if (day[r][c] < 10){

                        dia="0"+day[r][c];

                  }

                  else{

                        conditionalpadder = "";

                        dia=day[r][c];

                  }

                  if(month < 10) mes = "0"+month;

                  else mes=month;              

            targetwin.document.writeln("<a href=\"javascript:window.close();opener.document." + form.name + "." + dateFieldName + ".focus();" +          

            "opener.document." + form.name + "." + dateFieldName + ".value = '" + /*day[r][c]*/dia + "/" + /*month*/mes + "/" + year + "'" +             

            "\">" + conditionalpadder + day[r][c] + conditionalpadder + "</a>")

            }

            targetwin.document.writeln("</td>");

      }

      targetwin.document.writeln("</tr>");

}

 

targetwin.document.writeln("            </table>");

targetwin.document.writeln("          </td>");

targetwin.document.writeln("        </tr>");

targetwin.document.writeln("      </table>");

targetwin.document.writeln("      &nbsp;<br>");

targetwin.document.writeln("    </td>");

targetwin.document.writeln("  </tr>");

targetwin.document.writeln("</table>");

targetwin.document.writeln("</form>");

targetwin.document.writeln("</body>");

targetwin.document.writeln("</html>");

targetwin.document.close();

}
